package com.jacoblyu.factoryrunner.ui.views

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.VolumeUp
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import androidx.compose.ui.platform.LocalContext
import com.jacoblyu.factoryrunner.data.TestResultManager
import com.jacoblyu.factoryrunner.ui.theme.*

@Composable
fun AudioTestView(navController: NavController) {
    // 根据屏幕尺寸调整字体和间距
    val screenWidth = LocalConfiguration.current.screenWidthDp
    val screenHeight = LocalConfiguration.current.screenHeightDp
    
    // 根据屏幕尺寸调整字体大小
    val titleFontSize = if (screenWidth >= 1920) 48.sp else 32.sp
    val contentFontSize = if (screenWidth >= 1920) 36.sp else 24.sp
    val buttonFontSize = if (screenWidth >= 1920) 28.sp else 18.sp
    
    // 根据屏幕尺寸调整间距
    val spacing = if (screenWidth >= 1920) 32.dp else 24.dp
    val buttonHeight = if (screenWidth >= 1920) 80.dp else 60.dp
    val buttonWidth = if (screenWidth >= 1920) 200.dp else 150.dp
    
    // 测试状态
    var isPlayingSound by remember { mutableStateOf(false) }
    var isRecording by remember { mutableStateOf(false) }
    var micTestResult by remember { mutableStateOf("未测试") }
    var speakerTestResult by remember { mutableStateOf("未测试") }
    
    // 初始化TestResultManager
    val context = LocalContext.current
    val testResultManager = remember { TestResultManager(context) }
    
    // 麦克风喇叭测试状态
    var audioTestResult by remember { mutableStateOf(testResultManager.getTestResult("麦克风喇叭测试")) }
    
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        // 标题栏
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(spacing),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 上一步按钮
            // Home按钮
            IconButton(
                onClick = { navController.navigate("home") }
            ) {
                Icon(
                    imageVector = Icons.Filled.Home,
                    contentDescription = "返回主页",
                    tint = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.size(if (screenWidth >= 1920) 48.dp else 36.dp)
                )
            }
            
            // 标题
            Text(
                text = "音频测试",
                fontSize = titleFontSize,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.primary
            )
            
            // 下一步按钮
            IconButton(
                onClick = { navController.navigate("wifi_test") }
            ) {
                Icon(
                    imageVector = Icons.Default.ArrowForward,
                    contentDescription = "下一步",
                    tint = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.size(if (screenWidth >= 1920) 48.dp else 36.dp)
                )
            }
        }
        
        // 内容区域
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(spacing),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // 音频测试内容
            Card(
                modifier = Modifier
                    .fillMaxWidth(0.8f)
                    .height((screenHeight * 0.5f).dp),
                shape = RoundedCornerShape(16.dp),
                colors = CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.surface
                ),
                elevation = CardDefaults.cardElevation(
                    defaultElevation = 8.dp
                )
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(spacing),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.SpaceEvenly
                ) {
                    // 喇叭测试区域
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            text = "喇叭测试",
                            fontSize = contentFontSize,
                            fontWeight = FontWeight.Bold,
                            color = MaterialTheme.colorScheme.onSurface
                        )
                        
                        Spacer(modifier = Modifier.height(spacing))
                        
                        Row(
                            horizontalArrangement = Arrangement.spacedBy(spacing),
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Button(
                                onClick = { 
                                    isPlayingSound = !isPlayingSound
                                    speakerTestResult = if (isPlayingSound) "测试中..." else "已测试"
                                },
                                modifier = Modifier
                                    .width(buttonWidth)
                                    .height(buttonHeight),
                                colors = ButtonDefaults.buttonColors(
                                    containerColor = if (isPlayingSound) MaterialTheme.colorScheme.secondary 
                                                   else MaterialTheme.colorScheme.primary
                                )
                            ) {
                                Icon(
                                    imageVector = Icons.Default.VolumeUp,
                                    contentDescription = "播放测试音",
                                    modifier = Modifier.size(24.dp)
                                )
                                Spacer(modifier = Modifier.width(8.dp))
                                Text(
                                    text = if (isPlayingSound) "停止" else "播放",
                                    fontSize = buttonFontSize,
                                    color = MaterialTheme.colorScheme.onPrimary
                                )
                            }
                            
                            Text(
                                text = speakerTestResult,
                                fontSize = contentFontSize,
                                color = if (speakerTestResult == "已测试") Color.Green 
                                       else if (speakerTestResult == "测试中...") Color.Blue 
                                       else MaterialTheme.colorScheme.onSurface
                            )
                        }
                    }
                    
                    Divider(
                        modifier = Modifier
                            .fillMaxWidth(0.8f)
                            .height(1.dp),
                        color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.12f)
                    )
                    
                    // 麦克风测试区域
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            text = "麦克风测试",
                            fontSize = contentFontSize,
                            fontWeight = FontWeight.Bold,
                            color = MaterialTheme.colorScheme.onSurface
                        )
                        
                        Spacer(modifier = Modifier.height(spacing))
                        
                        Row(
                            horizontalArrangement = Arrangement.spacedBy(spacing),
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Button(
                                onClick = { 
                                    isRecording = !isRecording
                                    micTestResult = if (isRecording) "录音中..." else "已测试"
                                },
                                modifier = Modifier
                                    .width(buttonWidth)
                                    .height(buttonHeight),
                                colors = ButtonDefaults.buttonColors(
                                    containerColor = if (isRecording) MaterialTheme.colorScheme.error 
                                                   else MaterialTheme.colorScheme.primary
                                )
                            ) {
                                Text(
                                    text = if (isRecording) "停止录音" else "开始录音",
                                    fontSize = buttonFontSize,
                                    color = MaterialTheme.colorScheme.onPrimary
                                )
                            }
                            
                            Text(
                                text = micTestResult,
                                fontSize = contentFontSize,
                                color = if (micTestResult == "已测试") Color.Green 
                                       else if (micTestResult == "录音中...") Color.Red 
                                       else MaterialTheme.colorScheme.onSurface
                            )
                        }
                    }
                }
            }
            
            Spacer(modifier = Modifier.height(spacing))
            
            // 按钮区域
            Row(
                horizontalArrangement = Arrangement.spacedBy(spacing),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 不通过按钮
                Button(
                    onClick = { 
                        // 保存测试结果
                        testResultManager.saveTestResult("麦克风喇叭测试", "失败")
                        audioTestResult = "失败"
                        // 导航到下一个测试页面
                        navController.navigate("wifi_test?result=失败&test=麦克风喇叭测试")
                    },
                    modifier = Modifier
                        .width(buttonWidth)
                        .height(buttonHeight),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.error
                    )
                ) {
                    Text(
                        text = "不通过",
                        fontSize = buttonFontSize,
                        color = MaterialTheme.colorScheme.onError
                    )
                }
                
                // 通过按钮
                Button(
                    onClick = { 
                        // 保存测试结果
                        testResultManager.saveTestResult("麦克风喇叭测试", "通过")
                        audioTestResult = "通过"
                        // 导航到下一个测试页面
                        navController.navigate("wifi_test?result=通过&test=麦克风喇叭测试")
                    },
                    modifier = Modifier
                        .width(buttonWidth)
                        .height(buttonHeight),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.primary
                    )
                ) {
                    Text(
                        text = "通过",
                        fontSize = buttonFontSize,
                        color = MaterialTheme.colorScheme.onPrimary
                    )
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun AudioTestPreview() {
    FactoryrunnerTheme {
        AudioTestView(rememberNavController())
    }
}

@Preview(showBackground = true, widthDp = 1920, heightDp = 1200)
@Composable
fun AudioTestLargePreview() {
    FactoryrunnerTheme {
        AudioTestView(rememberNavController())
    }
}